package com.kaiyidiannao.model.req.model;

import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.req.base.BaseCommand;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 *   购物车 （post） 异步请求 - 添加商品
 */
public class PostShoppingCarAdd extends BaseCommand {

    private String TAG = "PostDayDayCouponShoppingCar";
    private String action = "add";//ajax-->add
    private String itemid;
    private String userid;
    private TreeMap<String,String> specesMap; // 商品的规格

    public PostShoppingCarAdd(String itemid, String userid, TreeMap<String,String> specesMap) {
        this.itemid = itemid;
        this.userid = userid;
        this.specesMap = specesMap;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.POST_DAY_DAY_COUPON_CART;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("action", action);
        paramters.put("itemid",itemid);
        paramters.put("userid",userid);

        // 添加规格
        Set<Map.Entry<String, String>> set = specesMap.entrySet();
        for (TreeMap.Entry<String,String> entry : set) {
            String index = entry.getKey();
            String selectedSpecIndex = entry.getValue();
            paramters.put("s" + index, selectedSpecIndex);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        DataCenter.getInstance().callBacks(DataCenter.flag.SHOPPING_CAR_ADD_SUCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        DataCenter.getInstance().callBacks(DataCenter.flag.SHOPPING_CAR_ADD_FAILED);
    }
}
